package com.ruoyi.web.controller.shop.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.CouponConstants;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.product.ShopProductAttributeDO;
import com.ruoyi.common.entity.VO.CouponSearchVO;
import com.ruoyi.common.entity.VO.CouponVO;
import com.ruoyi.common.entity.VO.member.MemberGetCreditProductVO;
import com.ruoyi.common.entity.VO.product.ProductSearchVO;
import com.ruoyi.common.entity.VO.product.RechargeProductVO;
import com.ruoyi.common.entity.VO.shop.ShopProductSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopProductVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.IBackShopProductMapper;
import com.ruoyi.web.controller.order.service.IBackOrderService;
import com.ruoyi.web.controller.shop.mapper.IBackShopProductAttrMapper;
import com.ruoyi.web.controller.shop.mapper.IBackShopProductCouponLinkMapper;
import com.ruoyi.web.controller.shop.service.IBackShopCouponService;
import com.ruoyi.web.controller.shop.service.IBackShopProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 店铺-商品管理
 */
@Controller
@RequestMapping("/back/shopProduct")
public class BackShopProductController extends BaseController {

    private final String prefix = "shop/product";

    @Resource
    private IBackShopProductService shopProductService;

    @Resource
    private IBackShopCouponService shopCouponService;

    @Resource
    private IBackOrderService backOrderService;

    @Resource
    private IBackShopProductCouponLinkMapper couponLinkMapper;

    @Resource
    private IBackShopProductAttrMapper backShopProductAttrMapper;

    @Resource
    private IBackShopProductMapper backShopProductMapper;

    /**
     * 页面-已添加的充值金额列表
     */
    @RequiresPermissions("back:shopProduct:memberRechargeList")
    @GetMapping("/memberRechargeList")
    public String memberRechargeList() {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        return prefix + "/memberRechargeList";
    }

    /**
     * 数据查询-站点充值列表（搜索）
     *
     * @param searchVO
     * @return
     */
    @RequiresPermissions("back:shopProduct:memberRechargeList")
    @PostMapping("/searchMemberRechargeList")
    @ResponseBody
    public TableDataInfo searchMemberRechargeList(ShopProductSearchVO searchVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        searchVO.setShopId(ShiroUtils.getShopId());
        searchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.RECHARGE.getValue());
        searchVO.setMemberVipTypeId(1L);
        startPage();
//        List<ShopProductVO> list = shopProductService.searchList(searchVO);
        List<RechargeProductVO> productList = backShopProductMapper.searchRechargeList(searchVO);
        return getDataTable(productList);
    }

    /**
     * 数据编辑-充值金额管理
     *
     * @return
     */
    @Log(title = "站点-充值金额管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:shopProduct:rechargeEdit")
    @PostMapping("/rechargeEdit")
    @ResponseBody
    public AjaxResult rechargeEdit(ShopProductVO shopProductVO) {
        shopProductVO.setSystemModule(SysConfigConstants.SystemModuleEnum.RECHARGE.getValue());
        return toAjax(shopProductService.edit(shopProductVO));
    }


    /**
     * 数据查询-校验油枪号
     */
    @PostMapping("/checkProductAttrNameUnique")
    @ResponseBody
    public String checkProductAttrNameUnique(ShopProductAttributeDO shopProductAttributeDO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        shopProductAttributeDO.setShopId(ShiroUtils.getShopId());
        return shopProductService.checkProductAttrNameUnique(shopProductAttributeDO);
    }

    /**
     * 页面-已添加的油品列表
     */
    @RequiresPermissions("back:shopProduct:petrolList")
    @GetMapping("/petrolList")
    public String petrolList() {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        return prefix + "/petrolList";
    }

    /**
     * 数据查询-站点油品列表（搜索）
     *
     * @param searchVO
     * @return
     */
    @RequiresPermissions("back:shopProduct:petrolList")
    @PostMapping("/searchPetrolList")
    @ResponseBody
    public TableDataInfo searchPetrolList(ShopProductSearchVO searchVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        searchVO.setShopId(ShiroUtils.getShopId());
        searchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.PETROL.getValue());
        startPage();
        List<ShopProductVO> list = shopProductService.searchList(searchVO);
        return getDataTable(list);
    }

    /**
     * 数据查询-站点油品列表（搜索）
     *
     * @param searchVO
     * @return
     */
    @RequiresPermissions("back:shopProduct:attrList")
    @PostMapping("/searchAttrList")
    @ResponseBody
    public TableDataInfo searchAttrList(ShopProductSearchVO searchVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        if(null == searchVO.getProductId() || 0 == searchVO.getProductId()){
            return getDataTable(new ArrayList<>());
        }
        searchVO.setShopId(ShiroUtils.getShopId());
        List<ShopProductAttributeDO> list = shopProductService.searchAttrList(searchVO);
        return getDataTable(list);
    }

    /**
     * 数据编辑-站点油品管理
     *
     * @return
     */
    @Log(title = "站点-油品管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:shopProduct:petrolEdit")
    @PostMapping("/petrolEdit")
    @ResponseBody
    public AjaxResult edit(ShopProductVO shopProductVO) {
        shopProductVO.setSystemModule(SysConfigConstants.SystemModuleEnum.PETROL.getValue());
        return toAjax(shopProductService.edit(shopProductVO));
    }


    /**
     * 页面-已添加的积分商品列表
     */
    @RequiresPermissions("back:shopProduct:creditList")
    @GetMapping("/creditList")
    public String creditList() {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        return prefix + "/creditList";
    }

    /**
     * 数据查询-站点积分商品列表（搜索）
     *
     * @param searchVO
     * @return
     */
    @RequiresPermissions("back:shopProduct:creditList")
    @PostMapping("/searchCreditList")
    @ResponseBody
    public TableDataInfo searchCreditList(ShopProductSearchVO searchVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        searchVO.setShopId(ShiroUtils.getShopId());
        searchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.CREDIT.getValue());
        startPage();
        List<ShopProductVO> list = shopProductService.searchList(searchVO);
        for (ShopProductVO shopProductVO : list
        ) {
            if (StringUtils.isNotEmpty(shopProductVO.getCoverUrl())) {
                shopProductVO.setCoverUrlThumb(shopProductVO.getCoverUrl().concat("-tf70"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 页面-新增积分商城商品
     *
     * @return
     */
    @RequiresPermissions("back:shopProduct:creditAdd")
    @GetMapping("/creditAdd")
    public String creditAdd(ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        CouponSearchVO searchVO = new CouponSearchVO();
        searchVO.setShopId(ShiroUtils.getShopId());
        searchVO.setCouponStatus(CouponConstants.CouponStatusEnum.AVAILABLE.getValue());
        mmap.put("couponList", shopCouponService.searchList(searchVO));
        return prefix + "/creditAdd";
    }

    /**
     * 数据处理-新增积分商城商品
     */
    @Log(title = "站点-商品管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("back:shopProduct:creditAdd")
    @PostMapping("/creditAdd")
    @ResponseBody
    public AjaxResult creditAdd(ShopProductVO shopProductVO) {
        shopProductVO.setShopId(ShiroUtils.getShopId());
        shopProductVO.setCreateBy(ShiroUtils.getSysUser().getUserName());
        shopProductVO.setCreateTime(new Date());
        shopProductService.add(shopProductVO);
        return AjaxResult.success();
    }

    /**
     * 页面-编辑积分商城商品
     */
    @RequiresPermissions("back:shopProduct:creditEdit")
    @GetMapping("/creditEdit/{shopProductId}")
    public String creditEdit(@PathVariable("shopProductId") Long shopProductId, ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        ShopProductVO shopProductInfo = shopProductService.getDetailById(shopProductId);
        CouponSearchVO searchVO = new CouponSearchVO();
        searchVO.setShopId(ShiroUtils.getShopId());
        searchVO.setCouponStatus(CouponConstants.CouponStatusEnum.AVAILABLE.getValue());
        List<CouponVO> couponList = shopCouponService.searchList(searchVO);
        if (couponList.size() > 0) {
            List<Long> couponIdArr = couponLinkMapper.searchIdList(searchVO.getShopId(), shopProductInfo.getProductId());
            for (CouponVO one : couponList) {
                for (Long couponId : couponIdArr) {
                    if (one.getId().equals(couponId)) {
                        one.setSelected(true);
                        break;
                    }
                }
            }
        }
        mmap.put("couponList", couponList);
        mmap.put("shopProduct", shopProductInfo);
        return prefix + "/creditEdit";
    }

    /**
     * 数据处理-编辑积分商城商品
     *
     * @return
     */
    @Log(title = "站点-积分商城商品管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:shopProduct:creditEdit")
    @PostMapping("/creditEdit")
    @ResponseBody
    public AjaxResult creditEdit(ShopProductVO shopProductVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        shopProductVO.setSystemModule(SysConfigConstants.SystemModuleEnum.CREDIT.getValue());
        shopProductVO.setShopId(ShiroUtils.getShopId());
        return toAjax(shopProductService.creditEdit(shopProductVO));
    }

    /**
     * 页面-新增积分商城商品
     *
     * @return
     */
    @RequiresPermissions("back:shopProduct:attrAdd")
    @GetMapping("/attrAdd")
    public String attrAdd(ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        ShopProductSearchVO searchVO = new ShopProductSearchVO();
        searchVO.setShopId(ShiroUtils.getShopId());
        searchVO.setSystemModule(SysConfigConstants.SystemModuleEnum.PETROL.getValue());
        List<ShopProductVO> petrolList = shopProductService.searchList(searchVO);
        petrolList.forEach(x->x.setProductName(x.getProductCategory()+x.getProductName()));
        mmap.put("petrolTypeList",petrolList);
        return prefix + "/attrAdd";
    }


    /**
     * 数据处理-新增油枪号
     */
    @Log(title = "站点-商品管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("back:shopProduct:attrAdd")
    @PostMapping("/attrAdd")
    @ResponseBody
    public AjaxResult attrAdd(ShopProductAttributeDO shopProductAttributeDO) {
        shopProductAttributeDO.setShopId(ShiroUtils.getShopId());
        shopProductAttributeDO.setCreateBy(ShiroUtils.getSysUser().getUserName());
        shopProductAttributeDO.setCreateTime(new Date());
        backShopProductAttrMapper.insertSelective(shopProductAttributeDO);
        return AjaxResult.success();
    }

    /**
     * 数据处理-编辑商品属性
     *
     * @return
     */
    @Log(title = "站点-编辑商品属性", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:shopProduct:attrEdit")
    @PostMapping("/attrEdit")
    @ResponseBody
    public AjaxResult attrEdit(ShopProductAttributeDO editVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        editVO.setShopId(ShiroUtils.getShopId());
        return toAjax(shopProductService.attrEdit(editVO));
    }

    /**
     * 页面-选择需要赠送的会员
     *
     * @return
     */
    @RequiresPermissions("back:shopProduct:sendToMember")
    @GetMapping("/selectMember/{productIdStr}")
    public String selectMember(@PathVariable("productIdStr") String productIdStr, ModelMap mmap) {
        mmap.put("productIdStr", productIdStr);
        return prefix + "/selectMember";
    }

    /**
     * 数据操作-批量赠送积分商品（直接到会员的兑换列表）
     *
     * @param memberGetCreditProductVO
     * @return
     */
    @Log(title = "站点-店铺优惠券赠送", businessType = BusinessType.INSERT)
    @RequiresPermissions("back:shopProduct:sendToMember")
    @PostMapping("/sendToMember")
    @ResponseBody
    public AjaxResult sendToMember(MemberGetCreditProductVO memberGetCreditProductVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        memberGetCreditProductVO.setShopId(ShiroUtils.getShopId());
        memberGetCreditProductVO.setCreateBy(ShiroUtils.getSysUser().getUserName());
        backOrderService.sendToMember(memberGetCreditProductVO);
        return success("赠送成功");
    }

    /**
     * 删除售卖商品
     */
    @Log(title = "店铺-商品管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("back:shopProduct:creditRemove")
    @PostMapping("/creditRemove")
    @ResponseBody
    public AjaxResult creditRemove(ShopProductVO shopProductVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        shopProductVO.setShopId(ShiroUtils.getShopId());
        return toAjax(shopProductService.creditRemove(shopProductVO));
    }

    /**
     * 删除商品规格
     */
    @Log(title = "站点-油枪号管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("back:shopProduct:attrRemove")
    @PostMapping("/attrRemove")
    @ResponseBody
    public AjaxResult attrRemove(ShopProductAttributeDO shopProductAttributeDO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        shopProductAttributeDO.setShopId(ShiroUtils.getShopId());
        return toAjax(shopProductService.attrRemove(shopProductAttributeDO));
    }

}
